package com.zeropoint.demo.api;



import com.zeropoint.demo.pojo.FormTotal;
import com.zeropoint.demo.pojo.WeightAlgorithm;
import com.zeropoint.demo.pojo.vo.Family;

import java.util.List;

// 评估
public class EvaluateService {

    public final static String  COMMON_FAMILY = "一般家庭";
    public final static String  EXTREMELY_NEEDY_FAMILY = "特困家庭";
    public final static String  BASICL_IVING_FAMILY = "低保家庭";
    public final static String  EXPENDITURE_BASED_POVERTY_FAMILY = "支出型贫困家庭";
    public final static String  EXPENDITURE_BASED_DIFFICULTY_FAMILY = "支出型贫困家庭";
    public final static String  LOW_MARGIN_FAMILY = "低保边缘家庭";
    private FormTotal formTotal;
    private Api api;
    private double v;

    public EvaluateService(FormTotal formTotal) {
        this.formTotal = formTotal;
        api = new Api(formTotal);
        this.v = api.getW().result;
    }

    public Api getApi() {
        return api;
    }

    public String evaluate(){

        // 评估流程
        WeightAlgorithm w = api.getW();
        // 返回结果
        return stepOne(w);
    }


    public String stepOne(WeightAlgorithm w){

        double equityInterest = w.stock;// 股票利息
        boolean isCar = w.isCar;// 汽车数量
        double housing_area = w.housingArea;// 住房面积
        // 第一步
        if(isCar  || equityInterest != 0 || housing_area > 80){
            if(v < 40){
                // 第二步判断
                return stepTwo(w);
            }else{
                return EvaluateService.COMMON_FAMILY;
            }
        }else{
            // 第二步判断
            return stepTwo(w);
        }

    }


    public String stepTwo(WeightAlgorithm w){
        double health = w.health;// 人均健康
        double durableGoods = w.durableGoods;// 耐用消费品
        // 第二步判断
        if(health == 1 && durableGoods == 1)
        {
            return EvaluateService.COMMON_FAMILY;
        }else{
            // 第三步判断
            return stepThree(w);
        }

    }

    public String stepThree(WeightAlgorithm w){

        int families = w.families;// 家庭人数
        int age = w.age;// 年龄
        double alimony = formTotal.getFormThree().transferIncome.alimony;// 赡养费

        if(families == 1 && age >= 60){
            // 是否有赡养费
            if(alimony != 0){
                // 第四步判断
                return stepFour(w);
            }else{
                if(v > 20){
                    // 第四步判断
                    return stepFour(w);

                }else return EvaluateService.EXTREMELY_NEEDY_FAMILY;
            }

        }else return stepFour(w);// 第四步判断


    }

    public String stepFour(WeightAlgorithm w){
        int families = w.families;// 家庭人数
        int age = w.age;// 年龄
        List<Family> family = formTotal.getFormOne().family;
        // if(families != family.size()) return "家庭人数信息不匹配";

        if(families == 1 && age < 60){

            String disabled = null;// 智力等级
            for (Family family1 : family) {
                // 应该只循环一次
                if(family1.disabled_info == null || family1.disabled_info.length == 0){
                    disabled = " ";
                }else{
                    for (String s : family1.disabled_info) {
                        disabled += s;
                    }
                }
            }
            if(disabled.contains("智力")){
                return EvaluateService.EXTREMELY_NEEDY_FAMILY;
            }else{
                if( disabled.contains("肢体") || disabled.contains("精神") || disabled.contains("视力")|| disabled.contains("听力")|| disabled.contains("言语")){
                    return EvaluateService.BASICL_IVING_FAMILY;
                }else{
                    // 第五步判断
                    return stepFive(w);
                }
            }

        }else{
            // 第五步判断
            return stepFive(w);
        }

    }

    public String stepFive(WeightAlgorithm w){

        // 第五步判断
        // 家庭人均收入 = 四种类型的收入 / 家庭总人口
        int totalPopulation = api.getI().getTotalPopulation();// 总人口
        double totalRevenue = api.getI().getTotalRevenue();// 四种类型收入之和
        double  people_average_income = totalRevenue / totalPopulation;// 家庭人均收入
        double minimum_living_level = formTotal.getQuestionnaire_info().minimum_living_level;// 当地最低生活保障


        if(people_average_income < minimum_living_level){
            return EvaluateService.BASICL_IVING_FAMILY;
        }else{
            // 第六步判断
            return stepSix(w);
        }


    }

    public String stepSix(WeightAlgorithm w){

        // 第六步判断
        int families = w.families;// 家庭人数
        double laborProportion = w.laborProportion;// 劳动力占比
        double employmentStatus = w.employmentStatus;// 就业状况
        int disabilitiesNumber = w.disabilitiesNumber;// 家庭残疾人数

        if(families == 2 && disabilitiesNumber != 0){
            if(disabilitiesNumber == 2){
                return EvaluateService.BASICL_IVING_FAMILY;
            }else{
                if(disabilitiesNumber == 1){
                    if(laborProportion == 0){
                        return EvaluateService.BASICL_IVING_FAMILY;
                    }else{
                        if(employmentStatus == 0){
                            return EvaluateService.BASICL_IVING_FAMILY;
                        }else{
                            // 第七步判断
                            return stepSeven(w);
                        }
                    }
                }else{
                    // 第七步判断
                    return stepSeven(w);
                }
            }
        }else{
            // 第七步判断
            return stepSeven(w);
        }

    }

    public String stepSeven(WeightAlgorithm w){
        // 第七步判断

        double health = w.health;// 人均健康状况
        int disabilitiesNumber = w.disabilitiesNumber;// 家庭残疾人数

        if(health == 0){

            if(disabilitiesNumber != 0){
                return EvaluateService.BASICL_IVING_FAMILY;
            }else{
                // 第八步判断f
                return stepEight(w);
            }
        }else{
            // 第八步判断
            return stepEight(w);
        }



    }


    public String stepEight(WeightAlgorithm w){

        // 第八步判断
        double bigHealthSpending = formTotal.getFormFour().necessaryExpenses.bigPersonMoney;// 重大疾病个人医疗支出

        if(bigHealthSpending > 15000) return EvaluateService.EXPENDITURE_BASED_POVERTY_FAMILY;
        else return stepNine(w);// 第九步判断

    }

    public String stepNine(WeightAlgorithm w){

        // 第九步判断
        double laborProportion = w.laborProportion;// 劳动力占比
        double employmentStatus = w.employmentStatus;// 就业状况
        double slowPersonMoney = formTotal.getFormFour().necessaryExpenses.slowPersonMoney;// 慢性疾病个人负担

        if(slowPersonMoney >= 1000){

            if(slowPersonMoney >= 5000){
                if(laborProportion == 0){
                    return EvaluateService.EXPENDITURE_BASED_DIFFICULTY_FAMILY;
                }else{
                    if(employmentStatus == 0){
                        return EvaluateService.BASICL_IVING_FAMILY;
                    }else{
                        return EvaluateService.LOW_MARGIN_FAMILY;
                    }
                }
            }else{
                if(slowPersonMoney >= 2000){
                    return EvaluateService.BASICL_IVING_FAMILY;
                }else{
                    // 第十步判断
                    return stepTen(w);
                }
            }

        }else{
            // 第十步判断
            return stepTen(w);
        }


    }

    public String stepTen(WeightAlgorithm w){

        // 第十步判断
        double slowPersonMoney = formTotal.getFormFour().suddenExpenditure.emergentEventMoney;// 非疾病突发性事件支出
        if(slowPersonMoney >= 3000) return EvaluateService.EXPENDITURE_BASED_POVERTY_FAMILY;
        else return stepEleven(w);



    }

    public String stepEleven(WeightAlgorithm w){

        // 第十一步判断
        double educationOutCome = formTotal.getFormFour().necessaryExpenses.educationOutCome;// 家庭子女每年教育支出

        if(educationOutCome >= 1000){

            if(educationOutCome >= 20000){
                return EvaluateService.EXPENDITURE_BASED_DIFFICULTY_FAMILY;
            }else{
                if(educationOutCome > 15000){
                    return EvaluateService.LOW_MARGIN_FAMILY;
                }else{
                    // 第十二步判断
                    return stepTwelve(w);
                }
            }

        }else {
            // 第十二步判断
            return stepTwelve(w);
        }


    }

    public String stepTwelve(WeightAlgorithm w){

        // 第十二步判断
        double perCapitaNetIncome = api.getO().perCapitaNetIncome;// 人均净收入
        double minimum_living_level = formTotal.getQuestionnaire_info().minimum_living_level;// 最低生活保障

        if(perCapitaNetIncome < 1.5 * minimum_living_level) return EvaluateService.EXPENDITURE_BASED_POVERTY_FAMILY;
        else return stepThirteen();



    }

    public String stepThirteen(){

        // 第十三步判断
        if(this.v <= 25) return EvaluateService.EXTREMELY_NEEDY_FAMILY;
        else return stepFourteen();

    }


    public String stepFourteen(){

        // 第十四步判断
        if(this.v <= 40) return  EvaluateService.BASICL_IVING_FAMILY;
        else return stepFifteen();

    }

    public String stepFifteen(){

        // 第十五步判断
        if(this.v <= 50) return  EvaluateService.LOW_MARGIN_FAMILY;
        else return stepSixteen();

    }


    public String stepSixteen(){

        // 第十六步判断
        return EvaluateService.COMMON_FAMILY;

    }



}
